﻿
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;


namespace Mes_app
{
    public partial class Login_Form : Form
    {
        public static string login_user_id; //声明静态变量userid
        public static string login_user_name;
        public static string login_station_no;
        public static string login_team_no;
        public static string login_team_name;
        public static string login_server;
        public static string login_database;
        public static string login_host_name;
        public static string login_computer_ip;


        public Login_Form()
        {
            InitializeComponent();


        }
        private void button1_Click(object sender, EventArgs e)
        {
            login_enter();
        }
        private void login_enter()
        {
            string password= "";
            login_user_id = TextUserid.Text.Trim();
      

            if (TextUserid.Text.Trim().Equals(""))
            {
                MessageBox.Show("用户ID不能为空！");
                return;
            }



            if (TextPassWord.Text.Trim().Equals(""))
            {
                MessageBox.Show("用户密码不能为空！");
                return;
            }
            //配置文档的存放地址
            string path = Application.StartupPath + "/config.ini";
            //写入
            Confighelper.WriteIni("login_user_id", "text", TextUserid.Text, path);
            Confighelper.WriteIni("login_user_text", "text", Label_userName.Text, path);


            //用户密码验证
            string sqlValue = "SELECT login_user_password " +
                " FROM login_user " +
                " WHERE login_user_id = @userid " +
                " AND active = 1 ";
            SqlParameter[] parameters = {
                  new SqlParameter("@userid",TextUserid.Text.Trim())
            };
            SqlDataReader dr = SqlHelper.ExecuteReader(sqlValue, parameters);
            while (dr.Read())
            {
                password = dr["login_user_password"].ToString();
               // sid = Convert.ToInt32(dr["id"].ToString());
            }
            dr.Close();

            if (TextPassWord.Text.Trim().Equals(password))
            {

                login_user_id = TextUserid.Text;
                login_user_name = Label_userName.Text;

                //获取登录用户主机名
                login_host_name = Dns.GetHostName();
                //获取登录用户IP地址
                var host = Dns.GetHostEntry(Dns.GetHostName());
                var local_ip = host.AddressList[1];
                login_computer_ip = local_ip.ToString();


                //跳转主界面
                this.DialogResult = DialogResult.OK;
                this.Dispose();
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                TextPassWord.PasswordChar = '\0';   //显示输入
            }
            else
            {
                TextPassWord.PasswordChar = '*';   //显示*
            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Close();
        }


        private void Login_Load(object sender, EventArgs e)
        {
         //   EncryptConnectionStringInConfigDPAPI();

            //回车监听
            this.KeyPreview = true;
            //读出
            string path = Application.StartupPath + "/config.ini";
            TextUserid.Text = Confighelper.ReadIni("login_user_id", "text", "", 1024, path);


          //  string str = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString;
           // MessageBox.Show(str);

            // string str = "server=192.168.0.253;database=Mes_app;uid=sa;pwd=sa";
           // string[] strArr = str.Split(';'); // 将字符串按照";"分割成多个子字符串
           // login_server = strArr[0]; // 取得分割后的第一个子字符串
          //  login_database = strArr[1]; // 取得分割后的第二个子字符串

          //  Label_Server.Text = login_server + ";" + login_database;

            // MessageBox.Show(subStr0 + ";" + subStr1);
            // Console.WriteLine(subStr0+"="+ subStr1); // 输出"def"

          //  string str2 = subStr0 + "=" + subStr1;
          //  string[] strArr2 = str2.Split('='); // 将字符串按照"="分割成多个子字符串
          //  string subStr_1 = strArr2[1]; // 取得分割后的第一个子字符串
          //  string subStr_3 = strArr2[3]; // 取得分割后的第二个子字符串

          //  MessageBox.Show(subStr_1 + "/" + subStr_3);
            TextPassWord.Focus();
        }



        public String Get_Userid2Text(String login_user_id)
        {
            String str_return = "";
            string sqlValue = "SELECT login_user_name FROM login_user WHERE login_user_id = @userid ";
            SqlParameter[] parameters = {
                  new SqlParameter("@userid",login_user_id)
                };

            SqlDataReader dr = SqlHelper.ExecuteReader(sqlValue, parameters);
            while (dr.Read())
            {
                str_return = dr["login_user_name"].ToString().Trim();

            };
            dr.Close();

            return str_return;
        }

        private void TextPassWord_KeyDown(object sender, KeyEventArgs e)
        {
            //if条件检测按下的是不是Enter键
            if (e.KeyCode == Keys.Enter)
            {
                login_enter();
            }
        }

        private void TextUserid_TextChanged(object sender, EventArgs e)
        {
            if (TextUserid.Text != "")
                Label_userName.Text = Get_Userid2Text(TextUserid.Text);

        }

        private void TextUserid_Leave(object sender, EventArgs e)
        {

        }

        private void Login_Form_Shown(object sender, EventArgs e)
        {
            TextPassWord.Focus();
        }
    }


}
